import * as allUrls from '@/utils/allUrls'
import { register,getCodeImg} from "../../api/login.js";
import {
	getTxUserSig
} from '../../api/liveHall.js'
	import {
		TimLogin,
	} from "../../api/tim.js"
export default {
    name: "login",
    data() {
        return {
            form: {
                username: '',
                password: '',
				code:'',
				uuid:''
            },
			codeUrl:'',
			captchaOnOff:true,
        }
    },
    inject: ['reload'],
	created() {
	  this.getCode();
	},
    methods: {
        doLogin() {
			this.$store.dispatch("Login", this.form).then(() => {			
				this.$store.dispatch('GetInfo').then(() => {
					getTxUserSig(this.$store.state.user.userid).then(res=>{
						if(res.code == 200){
							TimLogin(this.$store.state.user.userid.toString(),res.data)
						}
					})
				}).catch(err => {
				    store.dispatch('LogOut').then(() => {
				      Message.error(err)
				      next({ path: '/' })
				    })
				})
			      this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
			    }).catch(() => {
			      this.loading = false;
			        this.getCode();
			    });
        },
        //正则替换空格
        replaceSpace() {
            this.form.username = this.form.username.replace(/\s+/g, "");
            this.form.password = this.form.password.replace(/\s+/g, "");
            this.form.password = this.form.password.replace(/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im, "");
        },
        toHome() {
            this.$router.push("/")
        },
        toReset(){
            this.$router.push('/resetPas')
        },
		getCode(){
			getCodeImg().then(res => {
			  this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
			  if (this.captchaOnOff) {
			    this.codeUrl = "data:image/gif;base64," + res.img;
			    this.form.uuid = res.uuid;
			  }
			});
		}
    },
    mounted(){
    }
}
